﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Text.RegularExpressions;

namespace _0160922
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            setBtnEvent(char.Parse(((Button)sender).Text));
            showButtonText(char.Parse((((Button)sender).Text)));
        }

        string option = ""; //存放 运算符
        int pressNum = 0; // 连续运算时，是否先生成结果
        int pressPoint = 0; //连续按点号

        bool isSecond = false;//判断是否为第二次点击数字按钮
        bool isDone = false;//判断是否点击等于

        double num1 = 0;
        double num2 = 0;

        string textShow = "";

        /* 
         * 运算过程显示
         * @char text 按钮文本
         * 
         * return void
         * 
         */
        private void showButtonText(char text)
        {
            string inputText = text.ToString();
            if (inputText != "=")
            {
                if (inputText == "+" || inputText == "-" || inputText == "/" || inputText == "*")
                {
                    textShow = textBox1.Text;
                    lblShow.Text = textShow + inputText;
                }
            }
            else
            {
                if (isDone)
                {
                    textShow = "";
                    lblShow.Text = "";
                }
            }

        }

        /* 
         * 运算事件
         * @char c 按钮文本
         * 
         * return void
         * 
         */
        private void setBtnEvent(char c)
        {

            if (char.IsDigit(c))
            {
                if (isSecond)
                {
                    textBox1.Text = "";
                    isSecond = false;
                }
                textBox1.Text = textBox1.Text + c.ToString();
                isDone = false;
            }
            else if (c.ToString() == ".")
            {
                if (pressPoint >= 1)
                {
                    return;
                }
                if (textBox1.Text == "")
                {
                    textBox1.Text = "0.";
                }
                else
                {
                    if (isSecond)
                    {
                        textBox1.Text = "0.";
                        isSecond = false;
                    }
                    else
                    {
                        textBox1.Text += c;
                    }


                }
                pressPoint += 1;

            }
            else if (c.ToString() == "+" || c.ToString() == "-" || c.ToString() == "*" || c.ToString() == "/")
            {
                if (pressNum >= 1)
                {
                    calcNum(num1, double.Parse(textBox1.Text), true);
                }
                else {
                    option = c.ToString();
                }
                num1 = double.Parse(textBox1.Text);
                isSecond = true;
                isDone = false;
                pressNum += 1;
                pressPoint = 0;
            }
            else if (c.ToString() == "=")
            {
                if (isDone) //修复连续按等于号，结果被重复计算
                {
                    return;
                }
                if (num2 == 0)
                {
                    num2 = double.Parse(textBox1.Text);
                    calcNum(num1, double.Parse(textBox1.Text), false);
                }
                else
                {
                    calcNum(double.Parse(textBox1.Text), num1, false);
                }
                pressNum = 0; //解决1+1+1=3+1=6
                num2 = 0; // 解决 1+1=2-1=-1
                isDone = true;
                isSecond = true;
                pressPoint = 0;
            }
        }
        /* 
         * 清空按钮
         */
        private void btnAC_Click(object sender, EventArgs e)
        {
            option = ""; //存放 运算符
            pressNum = 0; // 连续运算时，是否先生成结果
            pressPoint = 0;

            isSecond = false;//判断是否为第二次点击数字按钮
            isDone = false;//判断是否点击等于

            num1 = 0;
            num2 = 0;

            textShow = "";
            textBox1.Text = "";
            lblShow.Text = "";
        }
        /* 
         * 计算过程
         * 
         * @double n1, double n2, bool isContinue
         * 数字1， 数字2， 是否先进行运算
         * return bvoid
         */
        private void calcNum(double n1, double n2, bool isContinue)
        {
            double result = 0;
            switch (option)
            {
                case "+":
                    result = n1 + n2;
                    break;

                case "-":
                    result = n1 - n2;
                    break;

                case "/":
                    result = n1 / n2;
                    break;

                case "*":
                    result = n1 * n2;
                    break;
            }
            textBox1.Text = result.ToString();
        }
        /* 
         * delete 功能
         */
        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text.StartsWith("-"))
            {
                textBox1.Text = "";
            }
            else
            {
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
            }
        }




    }
}